//
//  HBDateChooseView.m
//  HBLibary
//
//  Created by Rambo on 2019/6/8.
//

#import "HBDateChooseView.h"
#import "UIView+HBCategory.h"

@interface HBDateChooseView () <UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIView *topToolView;

@property (nonatomic, strong) UIButton *rightButton;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIButton *leftButton;

@property (nonatomic, strong) UIDatePicker *datePicker;

@property (nonatomic, copy) HBDateChooseViewHandler handler;

@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation HBDateChooseView

//- (instancetype)initWithFrame:(CGRect)frame{
//    if (self = [super initWithFrame:frame]) {
//        [self configUI];
//    }
//    return self;
//}

+ (instancetype)showWithTitle:(NSString *)title leftToolTitle:(NSString *)leftToolTitle rightToolTitle:(NSString *)rightToolTitle complete:(HBDateChooseViewHandler)complete{
    HBDateChooseView *view = [[self alloc] init];
    
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    [win endEditing:YES];
    
    view.frame = [UIScreen mainScreen].bounds;
    
    [view configUI];
    
    [win addSubview:view];
    
    if (title && ![title isEqualToString:@""]) {
        view.titleLabel.text = title;
    }

    if (leftToolTitle && ![leftToolTitle isEqualToString:@""]) {
        [view.leftButton setTitle:leftToolTitle forState:UIControlStateNormal];
    }

    if (rightToolTitle && ![rightToolTitle isEqualToString:@""]) {

        [view.rightButton setTitle:rightToolTitle forState:UIControlStateNormal];
    }else{
        [view.rightButton setTitle:@"确定" forState:UIControlStateNormal];
    }
    
    view.handler = complete;
    [view show];
    
    return view;
}

#pragma mark - set

- (void)setCanTouchDismiss:(BOOL)canTouchDismiss{
    _canTouchDismiss = canTouchDismiss;
    if (_canTouchDismiss) {
        if (!_tap) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
            tap.delegate = self;
            [self addGestureRecognizer:tap];
            self.tap = tap;
        }
    }else{
        [self removeGestureRecognizer:self.tap];
        self.tap = nil;
    }
}

- (void)setDatePickerLineColor:(UIColor *)datePickerLineColor{
    _datePickerLineColor = datePickerLineColor;
    
    for (UIView *subview in self.datePicker.subviews) {
        if (![subview isKindOfClass:NSClassFromString(@"_UIDatePickerView")]) continue;
        for (UIView *line in subview.subviews) {
            if (CGRectGetHeight(line.frame) <= 1.f) {
                line.backgroundColor = _datePickerLineColor;
            }
        }
        
    }
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    return touch.view == self;
}

#pragma mark - 初始化

- (void)configUI{
    UIView *superView = self;
    
    
    
    self.contentView = ({
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        [superView addSubview:view];
        
        view.translatesAutoresizingMaskIntoConstraints = NO;
        
        NSLayoutConstraint *layoutConstraintLeft = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintLeft];
        
        NSLayoutConstraint *layoutConstraintBottom = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintBottom];
        
        NSLayoutConstraint *layoutConstraintRight = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintRight];
        
        view;
    });

    superView = self.contentView;
    
    self.topToolView = ({
        UIView *view = [[UIView alloc] init];
        [superView addSubview:view];
        
        [view hb_drawBorderLineAtOption:(UIViewBorderLineOptionBottom) width:0.5 color:[UIColor colorWithRed:238.f/255.f green:238.f/255.f blue:238.f/255.f alpha:1]];
        
        view.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint *layoutConstraintLeft = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintLeft];
        
        NSLayoutConstraint *layoutConstraintTop = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintTop];
        
        NSLayoutConstraint *layoutConstraintRight = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintRight];
        
        NSLayoutConstraint *layoutConstraintHeight = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:44];
        [superView addConstraint:layoutConstraintHeight];
        
        
        view;
    });
    
    self.datePicker = ({
        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
        datePicker.datePickerMode = UIDatePickerModeDate;
        [superView addSubview:datePicker];
        datePicker.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint *layoutConstraintLeft = [NSLayoutConstraint constraintWithItem:datePicker attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintLeft];
        
        NSLayoutConstraint *layoutConstraintBottom = [NSLayoutConstraint constraintWithItem:datePicker attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintBottom];
        
        NSLayoutConstraint *layoutConstraintRight = [NSLayoutConstraint constraintWithItem:datePicker attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintRight];
        
        
        NSLayoutConstraint *layoutConstraintTop = [NSLayoutConstraint constraintWithItem:datePicker attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        [superView addConstraint:layoutConstraintTop];
        
        datePicker;
    });
}

#pragma mark - Action

- (void)dismiss{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(self.contentView.frame));
        self.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

- (void)show{
    
    self.contentView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight([UIScreen mainScreen].bounds));

    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.transform = CGAffineTransformIdentity;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    }];
}

- (void)done{
    !self.handler ? : self.handler(self.datePicker);
    [self dismiss];
}

#pragma mark - 懒加载

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor grayColor];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [self.topToolView addSubview:_titleLabel];
        
        _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        
        NSLayoutConstraint *layoutConstraintCenterX = [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
        [self.topToolView addConstraint:layoutConstraintCenterX];
        
        NSLayoutConstraint *layoutConstraintBottom = [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        [self.topToolView addConstraint:layoutConstraintBottom];
        
        
        NSLayoutConstraint *layoutConstraintTop = [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
        [self.topToolView addConstraint:layoutConstraintTop];
        
    }
    return _titleLabel;
}

- (UIButton *)leftButton{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_leftButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        _leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        _leftButton.translatesAutoresizingMaskIntoConstraints = NO;
        [self.topToolView addSubview:_leftButton];
        
        NSLayoutConstraint *layoutConstraintLeft = [NSLayoutConstraint constraintWithItem:_leftButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
        [self.topToolView addConstraint:layoutConstraintLeft];
        
        NSLayoutConstraint *layoutConstraintBottom = [NSLayoutConstraint constraintWithItem:_leftButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        [self.topToolView addConstraint:layoutConstraintBottom];
        
        
        NSLayoutConstraint *layoutConstraintTop = [NSLayoutConstraint constraintWithItem:_leftButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
        [self.topToolView addConstraint:layoutConstraintTop];
    }
    return _leftButton;
}

- (UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_rightButton addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
        _rightButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        _rightButton.translatesAutoresizingMaskIntoConstraints = NO;
        [self.topToolView addSubview:_rightButton];
        
        NSLayoutConstraint *layoutConstraintRight = [NSLayoutConstraint constraintWithItem:_rightButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
        [self.topToolView addConstraint:layoutConstraintRight];
        
        NSLayoutConstraint *layoutConstraintBottom = [NSLayoutConstraint constraintWithItem:_rightButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        [self.topToolView addConstraint:layoutConstraintBottom];
        
        
        NSLayoutConstraint *layoutConstraintTop = [NSLayoutConstraint constraintWithItem:_rightButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topToolView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
        [self.topToolView addConstraint:layoutConstraintTop];
    }
    return _rightButton;
}


@end
